﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Kaifa.SharePoint.RucCommon;

using System.Text;
using System.Collections;
using System.Web.Security;
using System.Security.Principal;
using System.DirectoryServices;
using Kaifa.SharePoint.RucCommon.DBAccess;    //需要先把DLL檔「加入參考」。

using Microsoft.Exchange.WebServices.Data;
using Microsoft.Exchange.WebServices.Auth;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using Microsoft.SharePoint.ApplicationPages.Calendar.Exchange;
using System.Threading;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.Entity;
using System.Data.EntityClient;
using System.Data.Objects;
using System.Data.SqlClient;
using System.Web.Configuration;


namespace UCWebpart.WEB
{
    public partial class BookingManage : System.Web.UI.Page
    {
        //管理员级别
        string userName;
        string orgType1;
        string orgType3;
        bool isAdmin;
        List<MTR_Admin> AdminInfoList = new List<MTR_Admin>();

        UserInfo userInfo = new UserInfo();

        protected void Page_Load(object sender, EventArgs e)
        {
            #region
            try
            {
                if (Application["EmailAddress"] == null)
                {
                    Application["EmailAddress"] = Worker.getEmail();
                }
                if (Session["userName"] != null)//|| request.getParameter("name")
                {
                    userName = Session["userName"].ToString();
                    if (string.IsNullOrWhiteSpace(userName))
                    {
                        Response.Redirect("../login.aspx", false);
                        return;
                    }
                }
                else if (Session["userName"] == null)
                {
                    Response.Redirect("../login.aspx", false);
                    return;
                }
                hidUserName.Value = userName;

                //获取会议室系统管理员级别
                using (var mtr_resEn = new meetingroomManagementEntities())
                {
                    adminL.Value = "0";
                    var cls = (from c in mtr_resEn.MTR_bas_Admin
                               where c.adminName == userName && c.Available == true
                               select c).ToList<MTR_bas_Admin>();
                    //不是管理员
                    if (cls.Count < 1)
                    {
                        adminL.Value = "0";
                        isAdmin = false;
                    }
                    else
                    {
                        isAdmin = true;
                        adminL.Value = "1";
                    }
                    foreach (var c in cls) {
                        MTR_Admin admins = new MTR_Admin();
                        admins.adminName = c.adminName;
                        admins.VenueType = c.VenueType;
                        admins.adminLevel = c.adminLevel;
                        admins.Available = c.Available;
                        admins.undefine1 = c.undefine1;
                        admins.undefine2 = c.undefine2;
                        AdminInfoList.Add(admins);
                    }
                }
            }
            catch (Exception ex)
            {
                Worker w = new Worker();
                w.writeErrorMessage("0", ex, "BookingManage_load");
                Response.Write("<script>alert('"+ex.Message +"');</script>");
            }
            #endregion 
            #region
            try
            {
                RUC r = new RUC();
                List<RucOrganizationExtend> userOrg = r.GetOrgLayeringBySamAccountNames(userName);

                foreach (RucOrganizationExtend org in userOrg)
                {
                    if (org.Organization.OrgTypeID == 1)
                    {
                        orgType1 = org.Organization.ABBR;
                        Session["orgType1"] = orgType1;
                        hidUserType1.Value = orgType1;
                        break;
                    }
                }
                foreach (RucOrganizationExtend org in userOrg)
                {
                    if (org.Organization.OrgTypeID == 3)
                    {
                        orgType3 = org.Organization.ABBR;
                        Session["orgType3"] = orgType3;
                        hidUserType3.Value = orgType3;
                        break;
                    }
                }
                userInfo.userName = userName;
                userInfo.Orgtype1 = orgType1;
                userInfo.Orgtype3 = orgType3;
                userInfo.AdminInfoList = AdminInfoList;
                userInfo.isAdmin = isAdmin;
                Session["userInfo"] = userInfo;
                if (orgType1.Contains("深圳") || orgType1.Contains("福田"))
                {
                    hidAdd.Value = "福田";
                }
                else if (orgType1.Contains("惠州"))
                {
                    hidAdd.Value = "惠州";
                }
                else if (orgType1.Contains("苏州"))
                {
                    hidAdd.Value = "苏州";
                }
                else if (orgType1.Contains("南山"))
                {
                    hidAdd.Value = "南山";
                }
                else if (orgType1.Contains("石岩") || orgType1.Contains("光磁"))
                {
                    hidAdd.Value = "石岩";
                }
                else if (orgType1.Contains("开发贸易"))
                {
                    hidAdd.Value = "福田";
                }
                else
                {
                    hidAdd.Value = "福田";
                }
            }
            catch (Exception ex)
            {
                Worker w = new Worker();
                w.writeErrorMessage("0", ex, "login");
                Response.Write("<script>alert('" + ex.Message + "'); </script>");
            }
            #endregion
            //Exception es = new Exception();
            //Worker ww = new Worker();
            //ww.writeErrorMessage("0", es, "bm");
        }
        
        
        protected void Button1_Click(object sender, EventArgs e)
        {
        }
    }
}